var ns;
var app = new Vue({
    el: '#app',
    data: {
        form: {
            keywords: '',
            exceptids: ''
        },
        list: [],
        source: 'chooseckproduct'
    },
    methods: {
        get_data: function() {
            var that = this;
            that.list = [];
            var options = {
                action: 'getckproductlist',
                keywords: that.form.keywords,
                exceptids: that.form.exceptids
            };
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    that.list = data.list;
                } else if (err) {
                    that.list = [];
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        do_search: function() {
            var that = this;
            that.get_data();
        },
        do_choose: function(item) {
            var that = this;
            item.ischecked = !item.ischecked;
        },
        do_save: function() {
            var that = this;
            var list = [];
            for (var i = 0; i < that.list.length; i++) {
                var item = that.list[i];
                for (var j = 0; j < item.children.length; j++) {
                    var quantity = Number(item.children[j].Quantity);
                    if (item.children[j].ischecked && quantity > 0) {
                        list.push(item.children[j]);
                    }
                }
            }
            if (list.length == 0) {
                api.toast({
                    msg: '请选择商品',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            api.sendEvent({
                name: 'do_choose_ckproduct_complete',
                extra: {
                    list: JSON.stringify(list)
                }
            });
            setTimeout(function() {
                api.closeWin();
            }, 500);
        }
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    ns.init_search();
    app.form.exceptids = api.pageParam.exceptids || ''
    app.get_data();
}
